/*
 * Copyright (c) 2010-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_OPENGL_GRAPHICS_H_INCLUDED_
#define ALKES_OPENGL_GRAPHICS_H_INCLUDED_

#include "alkes/gfx/IGraphics3D.h"
#include "alkes/opengl/GLWrapper.h"
#include "alkes/opengl/GLState.h"
#include "alkes/opengl/GLTextureUnit.h"
#include "alkes/opengl/IMatrixStack.h"

namespace alkes {

class GLImageBuffer;
class GLRenderTarget;
class GLVertexBuffer;
class GLVertexBufferVBO;
class SharedLibrary;

class AL_OGL_EXPORT GLGraphics
    : public IGraphics3D
{
    AL_DECLARE_CLASS(GLGraphics, IGraphics3D);

public:
    static GLGraphics* create(uint16_t width, uint16_t height, TargetBehavior behavior = AL_BEHAVIOR_DOUBLE);

    // derived from IGraphics class.
    virtual void resize(uint16_t width, uint16_t height);
    virtual void flip();
    virtual DecoderFactory* createDecoderFactory();
    virtual ImageBufferFactory* createImageBufferFactory();

    // derived from IGraphics2D class.
    virtual IRenderTarget2D* createDefaultRenderTarget2D();

    // derived from IGraphics3D class.
    virtual IRenderTarget3D* createDefaultRenderTarget3D();
    virtual IVertexBuffer* createVertexBuffer(uint32_t vertex_type);

    // bridge interface
    void beginDraw2D(GLRenderTarget* target);
    void endDraw2D();
    void beginDraw3D(GLRenderTarget* target);
    void endDraw3D();

    void clearBuffer(uint32_t buffer_flag, const Color& clear_color, const Rect* rect, float depth);

    void setScissorRect(const Rect& rect);
    const Rect getScissorRect() const;
    void setViewport(const Viewport& viewport);
    const Viewport& getViewport() const;
    void setPerspective(float angle, float aspect, float znear, float zfar);
    void setLookAt(const Vector3D& eye, const Vector3D& at, const Vector3D& up);
    void translate(float x, float y, float z);
    void scale(float xscale, float yscale, float zscale);
    void rotate(float angle, float x, float y, float z);

    void setState(int state, bool enable);
    bool getState(int state);
    void setAlphaBlendMode(AlphaBlendMode src_blend, AlphaBlendMode dst_blend);
    void getAlphaBlendMode(AlphaBlendMode* src_blend, AlphaBlendMode* dst_blend) const;

    bool setTexture(Image* image, int stage);
    void setTextureFilter(TexFilterValue min_filter, TexFilterValue mag_filter, int stage);
    void getTextureFilter(TexFilterValue* min_filter, TexFilterValue* mag_filter, int stage);
    void setTextureAddressMode(TexAddressMode u_mode, TexAddressMode v_mode, int stage);
    void getTextureAddressMode(TexAddressMode* u_mode, TexAddressMode* v_mode, int stage);

    bool drawPrimitive(IVertexBuffer* vbuffer, PrimitiveType type, uint32_t start, uint32_t count);

    void pushMatrix();
    void popMatrix();
    void translate(float x, float y);
    void scale(float xscale, float yscale, const Vector2D& origin = Vector2D::ZERO);
    void rotate(float angle, const Vector2D& origin = Vector2D::ZERO);
    void skewX(float angle);
    void skewY(float angle);

    int32_t getGLYPos(int32_t window_y, int32_t rect_height) const;
    int32_t getAlkesYPos(int32_t window_y, int32_t rect_height) const;

    void bindTexture(GLenum target, GLuint texture);
    void genBuffers(GLsizei num, GLuint* buffers);
    void bindBuffer(GLenum target, GLuint buffer, bool& modified);
    void bufferData(GLenum target, GLsizeiptr size, const GLvoid* data, GLenum usage);

    bool supportsShader() const;
    bool supportsVBO() const;
    bool isActiveShader() const;

protected:
    GLGraphics(uint16_t width, uint16_t height, TargetBehavior behavior);
    virtual ~GLGraphics();

    virtual void destroy();

    GLState& getStateContainer();
    const GLState& getStateContainer() const;

    enum OpenGLOption
    {
        AL_OGLOPT_FEATURE_VBO,
        AL_OGLOPT_FEATURE_FBO,
        AL_OGLOPT_FEATURE_SHADER,
    };

    uint16_t getWidth() const;
    uint16_t getHeight() const;

    void setOption(OpenGLOption option, bool value);
    bool getOption(OpenGLOption option) const;

private:
    Fixed16 gl_version_;
    Fixed16 shader_version_;
    uint16_t width_, height_;
    TargetBehavior behavior_;
    GLState state_;
    GLTextureUnit texture_unit_;
    TBitArray<uint32_t> extension_flag_;
    GLRenderTarget* current_target_;
    Viewport viewport_;
    intrusive_ptr<ImageBufferFactory> image_factory_;
    SharedLibrary* shared_;
    DecoderFactory* (*codec_factory_)(ImageBufferFactory*);
    IMatrixStack* matrix_stack_;

    GLuint gl_texture_;
    GLuint gl_array_buffer_;
    GLuint gl_element_array_buffer_;
};

}

#endif
